/*  Copyright (c) 2012 William Rogers and James Boud

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using System;
//using SurfaceFormat = Microsoft.Xna.Framework.Graphics.SurfaceFormat;
//using DepthFormat = Microsoft.Xna.Framework.Graphics.DepthFormat;
//using RenderTargetUsage = Microsoft.Xna.Framework.Graphics.RenderTargetUsage;
using SlimDX;
using SlimDX.Direct3D11;

namespace JBBRXG11
{
    /// <summary>
    /// Defines the window dimensions of a render-target surface onto which a 3D
    /// volume projects.
    /// </summary>
    public struct Viewport
    {
        SlimDX.Direct3D11.Viewport dxviewport;

        internal SlimDX.Direct3D11.Viewport DXViewport { get { return dxviewport; } }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////// Constructors ////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Creates an instance of this object.
        /// </summary>
        /// <param name="x">The x coordinate of the upper-left corner of the viewport
        /// in pixels.</param>
        /// <param name="y">The y coordinate of the upper-left corner of the viewport
        /// in pixels.</param>
        /// <param name="width">The width of the viewport in pixels.</param>
        /// <param name="height">The height of the viewport in pixels.</param>
        public Viewport(int x, int y, int width, int height)
        {
            dxviewport = new SlimDX.Direct3D11.Viewport(x, y, width, height);
            dxviewport.MinZ = 0;
            dxviewport.MaxZ = 1;
        }

        /// <summary>
        /// Creates an instance of this object.
        /// </summary>
        /// <param name="bounds">A bounding box that defines the location and size of
        /// the viewport in a render target.</param>
        public Viewport(Microsoft.Xna.Framework.Rectangle bounds)
        {
            dxviewport = new SlimDX.Direct3D11.Viewport(bounds.X, bounds.Y, bounds.Width, bounds.Height);
            dxviewport.MinZ = 0;
            dxviewport.MaxZ = 1;
        }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////// Properties //////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Gets or sets the pixel coordinate of the upper-left corner of the
        /// viewport on the render-target surface.
        /// </summary>
        public int X { get { return (int)dxviewport.X; } set { dxviewport.X = value; } }

        /// <summary>
        /// Gets or sets the pixel coordinate of the upper-left corner of the
        /// viewport on the render-target surface.
        /// </summary>
        public int Y { get { return (int)dxviewport.Y; } set { dxviewport.Y = value; } }

        /// <summary>
        /// Gets or sets the width dimension of the viewport on the render-target
        /// surface, in pixels.
        /// </summary>
        public int Width { get { return (int)dxviewport.Width; } set { dxviewport.Width = value; } }

        /// <summary>
        /// Gets or sets the height dimension of the viewport on the render-target
        /// surface, in pixels.
        /// </summary>
        public int Height { get { return (int)dxviewport.Height; } set { dxviewport.Height = value; } }

        /// <summary>
        /// Gets or sets the minimum depth of the clip volume.
        /// </summary>
        public float MinDepth { get { return dxviewport.MinZ; } set { dxviewport.MinZ = value; } }

        /// <summary>
        /// Gets or sets the maximum depth of the clip volume.
        /// </summary>
        public float MaxDepth { get { return dxviewport.MaxZ; } set { dxviewport.MaxZ = value; } }

        /// <summary>
        /// Gets the size of this resource.
        /// </summary>
        public Microsoft.Xna.Framework.Rectangle Bounds
        {
            get
            {
                return new Microsoft.Xna.Framework.Rectangle((int)dxviewport.X, (int)dxviewport.Y, (int)dxviewport.Width, (int)dxviewport.Height);
            }
            set
            {
                dxviewport.X = value.X; dxviewport.Y = value.Y; dxviewport.Width = value.Width; dxviewport.Height = value.Height;
            }
        }

        /// <summary>
        /// Gets the aspect ratio used by the viewport
        /// </summary>
        public float AspectRatio { get { return (float)dxviewport.Width / (float)dxviewport.Height; } }

        /// <summary>
        /// Returns the title safe area of the current viewport.
        /// </summary>
        public Microsoft.Xna.Framework.Rectangle TitleSafeArea { get { return Bounds; } }

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //////////////////////////////////////// Methods /////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public new String ToString()
        {
            return "Viewport: (" + ((int)dxviewport.X).ToString() + "," + ((int)dxviewport.Y).ToString()
                           + "," + ((int)dxviewport.Width).ToString() + "," + ((int)dxviewport.Height).ToString() + ")";
        }

        /// <summary>
        /// Projects a 3D vector from object space into screen space.
        /// </summary>
        /// <param name="source">The vector to project.</param>
        /// <param name="projection">The projection matrix.</param>
        /// <param name="view">The view matrix.</param>
        /// <param name="world">The world matrix.</param>
        public Vector3 Project(Vector3 source, Matrix projection, Matrix view, Matrix world)
        {
            Vector4 temp = Vector4.Transform(new Vector4(source, 1), world * view * projection);
            Vector3 result = new Vector3(temp.X / temp.W, temp.Y / temp.W, temp.Z / temp.W);
            return new Vector3((result.X + 1) / 2 * dxviewport.Width, (result.Y + 1) / 2 * dxviewport.Height, result.Z);
        }

        /// <summary>
        /// Converts a screen space point into a corresponding point in world space.
        /// </summary>
        /// <param name="source">The vector to project.</param>
        /// <param name="projection">The projection matrix.</param>
        /// <param name="view">The view matrix.</param>
        /// <param name="world">The world matrix.</param>
        public Vector3 Unproject(Vector3 source, Matrix projection, Matrix view, Matrix world)
        {
            Vector4 temp = new Vector4(source.X / dxviewport.Width * 2 - 1, source.Y / dxviewport.Height * 2 - 1, source.Z, 1);
            Vector4 result = Vector4.Transform(temp, Matrix.Invert(world * view * projection));
            return new Vector3(result.X / result.W, result.Y / result.W, result.Z / result.W);
        }
    }
}
